package net.yxsoft.controller.pc.sysmgt;

import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.ext.interceptor.POST;
import net.sf.ehcache.search.parser.ParseException;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.model.formdesigner.FnFormDataRole;
import net.yxsoft.model.formdesigner.Form;
import net.yxsoft.model.formdesigner.FormDataSource;
import net.yxsoft.model.formdesigner.FormField;
import net.yxsoft.model.infrastructure.TsMenu;
import net.yxsoft.service.formdisigner.*;

/**
 * @author zhaohaibo
 */
public class FormDesignerController extends AuthController {

  @Inject
  FormService fsv;
  @Inject
  FormDataSourceService dsv;
  @Inject
  FormFieldService ffsv;
  @Inject
  FormDataService fdsv;
  @Inject
  FormDataRoleService rsv;

  public void form() {
    render("form.html");
  }

  public void dataSource() {
    render("datasource.html");
  }

  //region 数据源维护

  /**
   * pid为空，则查找根节点
   * state为0或1
   */
  @Before(POST.class)
  public void findDataSource() {
    renderJson(dsv.find(getParamKeyMap()));
  }

  @Before(POST.class)
  public void addDataSource() {
    renderJson(dsv.add(getModel(FormDataSource.class, "")));
  }

  @Before(POST.class)
  public void updateDataSource() {
    renderJson(dsv.update(getModel(FormDataSource.class, "")));
  }

  @Before(POST.class)
  public void toggleDisabledDataSource() {
    renderJson(dsv.toggleDisabled(get("id"), get("state")));
  }

/*  @Before(POST.class)
  public void removeDataSource() {
    renderJson(FormDataSourceService.SVC.remove(get("id")));
  }*/

  //endregion

  //region 表单维护
  @Before(POST.class)
  public void findForm() {

    renderJson(fsv.find(getParamKeyMap()));
  }

  /**
   * 获取绑定模型
   */
  public void getImportModelTable() {

    renderJson(fsv.getImportModelTable(getPara("form_id")));
  }


  @Before(POST.class)
  public void addForm() {
    renderJson(fsv.add(getModel(Form.class, "")));
  }

  @Before(POST.class)
  public void updateForm() {
    renderJson(fsv.update(getModel(Form.class, "")));
  }

  @Before(POST.class)
  public void toggleDisabledForm() {
    renderJson(fsv.toggleDisabled(get("id"), get("state")));
  }

/* @Before(POST.class)
  public void removeForm() {
    renderJson(FormService.SVC.remove(get("id")));
  }*/
  //endregion

  //region 表单字段结构维护
  @Before(POST.class)
  public void findFormFields() {
    renderJson(ffsv.find(getParamKeyMap()));
  }

  @Before(POST.class)
  public void addFormField() {
    renderJson(ffsv.add(getModel(FormField.class, "")));
  }

  @Before(POST.class)
  public void updateFormField() {
    renderJson(ffsv.update(getModel(FormField.class, "")));
  }

  @Before(POST.class)
  public void toggleDisabledField() {
    renderJson(ffsv.toggleDisabled(get("id"), get("state")));
  }

  public void findCascadeFileds(){

    renderJson(ffsv.findCascadeFileds(getPara("formid")));
  }
  //endregion

  public void getAttribList(){
    renderJson(fdsv.getAttribList(getPara("formid")));
  }

  /**
   * 创建菜单的数据源   动态表单类的菜单
   */
  public void getMenusDatasource(){
    renderJson(fdsv.getMenusDatasource());
  }


  /**
   * 保存动态表单 菜单
   * */
  public void saveExampleMenu() {

    String exampleName = getPara("exampleName");
    String exampleCode = getPara("exampleCode");
    renderJson(fdsv.saveExampleMenu(getModel(TsMenu.class, "", true), exampleName, exampleCode, getLoginUser()));
  }

  /**
   * 获取一级动态表单
   */
  public void getParentForms(){

    renderJson(fdsv.getParentForms());
  }

  //region 动态表单操作角色

  public void getDataRoleList(){renderJson(fdsv.getDataRoleList(getPara("row_id")));}

  public void addRoles() throws ParseException {
    renderJson(rsv.add(getModel(FnFormDataRole.class, "", true),getLoginUser()));
  }

  public void updateRoles() throws ParseException {
    renderJson(rsv.update(getModel(FnFormDataRole.class, "", true),getLoginUser()));
  }

  public void removeRoles()  throws ParseException{
    renderJson(rsv.remove(get("ids"), getLoginUser()));
  }



  /**
   * 获取动态表单上链方式
   */
  public void getBlockChainMode(){

    renderJson(fsv.getBlockChainMode());
  }
  /**
   * 获取模型
   */
  public void getModel(){

    renderJson(fsv.getModel());
  }


  //endregion

}
